﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Security.AccessControl;
using System.Configuration;
using System.Data.Common;
using System.Windows.Forms;
using System.Data;
namespace BuddhistSchools.DTO
{
    public class ConnectionSQL
    {
        private string _SeverName = "";
        private bool _Authentication = false;
        private string _UserName = "";
        private string _Password = "";
        private string _DatabaseName = "";
        public ConnectionSQL(string severName, string databaseName)
        {
            SeverName = severName;
            DatabaseName = databaseName;
            Authentication = true;
        }
        public ConnectionSQL(string severName, string databaseName, string userName, string password)
        {
            SeverName = severName;
            DatabaseName = databaseName;
            UserName = userName;
            Password = password;
        }
        public ConnectionSQL()
        { }
        public string ConnectionString()
        {
            SqlConnectionStringBuilder str = new SqlConnectionStringBuilder();
            str.DataSource = SeverName;
            str.InitialCatalog = DatabaseName;
            if (Authentication)
            {
                str.IntegratedSecurity = Authentication;
            }
            else
            {
                str.UserID = UserName;
                str.Password = Password;
            }
            return str.ConnectionString;
        }

        public string DatabaseName
        {
            get { return _DatabaseName; }
            set { _DatabaseName = value; }
        }

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        public bool Authentication
        {
            get { return _Authentication; }
            set { _Authentication = value; }
        }

        public string SeverName
        {
            get { return _SeverName; }
            set { _SeverName = value; }
        }
        public static bool TestConnection()
        {
            try
            {
                SqlConnection _sqlConn;
                Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                string _StrConSQL = _config.ConnectionStrings.ConnectionStrings["dbBuddhistSchoolsContext"].ConnectionString;
                _sqlConn = new SqlConnection(_StrConSQL);
                if (_sqlConn.State != ConnectionState.Open) _sqlConn.Open();
                if (_sqlConn.State != ConnectionState.Closed)
                {
                    _sqlConn.Close();
                    _sqlConn.Dispose();

                }
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static bool TestConnection(ConnectionSQL conSQL)
        {
            try
            {
                SqlConnection _sqlConn;
                string _StrConSQL = conSQL.ConnectionString();
                _sqlConn = new SqlConnection(_StrConSQL);
                if (_sqlConn.State != ConnectionState.Open) _sqlConn.Open();
                if (_sqlConn.State != ConnectionState.Closed)
                {
                    _sqlConn.Close();
                    _sqlConn.Dispose();

                }
                return true;
            }
            catch
            {
                return false;
            }
        }
       
    }
}
